var temp
$(document).ready(function() {
	$("#contentComment").keydown(
			function(event) {
				if (event.keyCode == 13) {		
					var contentCM = $("#contentComment").val()
					//alert("post comment")
					if(contentCM!=""){
						$.ajax({
							url : "saveCommentInMyPlacePage",
							type : 'GET',
							data : {
								cm : contentCM,
								placeID : temp,
							},
							dataType : 'JSON',
							success : function(response) {
								var result = response;
								var divComment = $("#listAllComment")
								var divTemp = "<div>" + result.userName + ":&nbsp;" + contentCM + "</div>"			
								$(divComment).append(divTemp);					
								$("#contentComment").val("");
								return false;
							},
							error : function(data) {
								return false;
							}
						});
						event.preventDefault();
					}else{
						alert("Content cannot null!")
					}
			}
	});
});

function clickOnPlaceItem(e) {
	var pID=e.id
	$("#listAllCommentsub").html('');
	$("#listAllOldComment").html('');
	$(".superContainer").show();
	temp = e.id;
	var htmldata = '';
	$.ajax({
		url : "placeDetail",
		type : "GET",
		data : "placeID="+temp,
		success:function(response){
			var result = response;
			$("#imageplace").html('<img src="/Lemon/Avatar/getAvatar?placeID=' + temp +  '" width=470px height=260px />')
			$("#addressValue").html("Type: "+result.propertytype+"<br> Address: "+result.address+"<br>Bed: "+result.bed+"<br> Bath: "+result.bath+"<br>Price: "+result.price +"<br>Description: "+result.description);
			$("#contactplace").html(result.nameContact+"<br> Phone: "+result.phoneContact+"<br> Email: "+result.emailContact);
		}
	});
	 
	$.ajax({
		url : "listAllComment",
		type : 'GET',
		data : {
			placeid : temp
		},
		contentType : "application/json; charset=utf-8",
		success : function(data) {
			var newest = $("#listAllOldComment");
			for ( var i = 0; i < data.length; i++) {
				var divTempCM = "<div> " + data[i].userprofile.fullName + ": " + data[i].content + "<br></div>"
				$(newest).append(divTempCM);
			}
		},
		error : function(data) {
		}
	});
	$.ajax({
		url : "allLikeInMyPlace",
		type : 'GET',
		data : {
			placeid : temp
		},
		contentType : "application/json; charset=utf-8",
		dataType : 'JSON',
		success : function(response) {
			var result = response;
			var tempLike = result.totalOfLike;
			$("#countOflike").html(tempLike);
		},
		error : function(data) {
		}
	});

	$.ajax({
		url : "allLikeinMyPlace",
		type : 'GET',
		data : {
			placeid : temp
		},
		contentType : "application/json; charset=utf-8",
		dataType : 'JSON',
		success : function(response) {
			var result = response;
			var tempLike = result.totalOfLike;
			$("#countOflike").html(tempLike);
		},
		error : function(data) {
		}
	});
	
	$.ajax({
		url : "liked",
		type : 'GET',
		data : {
			placeid : temp
		},
		contentType : "application/json; charset=utf-8",
		dataType : 'JSON',
		success : function(data) {
			if (data.result == "yes") {
				$("#dislike").show();
				$("#like").hide();
			} else {
				$("#like").show();
				$("#dislike").hide();
			}
		},
		error : function(data) {
		}
	});
	
	listimage(pID)
}
function listimage(pID)
{   
	var htmlData='';
	var data = [];
	var image = new Object;
	
	$.ajax({
		//url : "${g.createLink(controller:'Image',action:'listImage')}",
		//url  : "${g.createLink(controller:'Image',action:'listImage')}",
		url  : "listImage",
		type : "GET",
		data : "placeID="+pID,
		success:function(response){
			if(response.length > 0) {
				for (var i = 0; i < response.length; i++) {
					image.image='showImage?placeID=' + pID + '&index=' + i;
					data.push(image);
					image = new Object;
				}
				$('#galleria').galleria({
				  transition: 'fade' 
				});
		        Galleria.run('#galleria', {dataSource: data, autoplay: 2000, wait:true, debug:true,});
		  }
		}
	});
}
function isLikeInMyPlace(e) {
	var txt = e.id;
	if (txt == "like") {
		$("#like").hide();
		$("#dislike").show();
		$.ajax({
			url : "isLikeInMyPlace",
			type : 'GET',
			data : {
				placeid : temp,
				testLike : true,
			},
			contentType : "application/json; charset=utf-8",
			dataType : 'JSON',
			success : function(response) {
				var result = response;
				var tempLike = result.totalLike;
				$("#countOflike").html(tempLike);
			},
			error : function(data) {
			}
		});

	}
	if (txt == "dislike") {
		$("#dislike").hide();
		$("#like").show();

		$.ajax({
			url : "isDisLikeInMyPlace",
			type : 'GET',
			data : {
				placeid : temp,
				testLike : true,
			},
			contentType : "application/json; charset=utf-8",
			dataType : 'JSON',
			success : function(response) {
				var result = response;
				var tempLike = result.disLike;
				$("#countOflike").html(tempLike);
			},
			error : function(data) {
			}
		});
	}
}